package com.fr.cell.core.date;

import java.awt.Toolkit;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.JTextComponent;
import javax.swing.text.PlainDocument;

public class JDateDocument extends PlainDocument
{
  private JTextComponent textComponent;
  private SimpleDateFormat dateFormat;

  public JDateDocument(JTextComponent paramJTextComponent, SimpleDateFormat paramSimpleDateFormat)
    throws UnsupportedOperationException
  {
    this(paramJTextComponent, paramSimpleDateFormat, getCurrentDate(paramSimpleDateFormat));
  }

  public JDateDocument(JTextComponent paramJTextComponent, SimpleDateFormat paramSimpleDateFormat, String paramString)
    throws UnsupportedOperationException
  {
    setDateFormat(paramSimpleDateFormat);
    this.textComponent = paramJTextComponent;
    try
    {
      insertString(0, paramString, null);
    }
    catch (BadLocationException localBadLocationException)
    {
      throw new UnsupportedOperationException(localBadLocationException.getMessage());
    }
  }

  public void setDateFormat(SimpleDateFormat paramSimpleDateFormat)
  {
    this.dateFormat = paramSimpleDateFormat;
  }

  public SimpleDateFormat getDateFormat()
  {
    return this.dateFormat;
  }

  public static String getCurrentDate(SimpleDateFormat paramSimpleDateFormat)
  {
    return paramSimpleDateFormat.format(new Date());
  }

  public void insertString(int paramInt, String paramString, AttributeSet paramAttributeSet)
    throws BadLocationException
  {
    if (paramString.length() == 1)
    {
      try
      {
        Integer.parseInt(paramString);
      }
      catch (Exception localException)
      {
        Toolkit.getDefaultToolkit().beep();
        return;
      }
      int i = paramInt;
      if ((paramInt == 4) || (paramInt == 7) || (paramInt == 10) || (paramInt == 13) || (paramInt == 16))
        this.textComponent.setCaretPosition(++i);
      if (paramInt == this.dateFormat.toPattern().length())
        return;
      String str = this.textComponent.getText();
      str = str.substring(0, i) + paramString + str.substring(i + 1);
      boolean bool = isValidDate(str);
      if (!(bool))
      {
        Toolkit.getDefaultToolkit().beep();
        return;
      }
      super.remove(i, 1);
      super.insertString(i, paramString, paramAttributeSet);
    }
    else if ((paramString.length() == 10) || (paramString.length() == 19))
    {
      if (!(isValidDate(paramString)))
      {
        Toolkit.getDefaultToolkit().beep();
        return;
      }
      super.remove(0, getLength());
      super.insertString(0, paramString, paramAttributeSet);
    }
  }

  public void remove(int paramInt1, int paramInt2)
    throws BadLocationException
  {
    if ((paramInt1 == 4) || (paramInt1 == 7) || (paramInt1 == 10) || (paramInt1 == 13) || (paramInt1 == 16))
      this.textComponent.setCaretPosition(paramInt1 - 1);
    else
      this.textComponent.setCaretPosition(paramInt1);
  }

  private boolean isValidDate(String paramString)
  {
    int i;
    int j;
    int k;
    int l = 0;
    int i1 = 0;
    int i2 = 0;
    int i4 = getDateFormat().toPattern().length();
    if (paramString == null)
      return false;
    paramString = paramString.trim();
    if (paramString.length() != i4)
      return false;
    for (int i5 = 0; i5 < 10; ++i5)
      if (paramString.charAt(i5) > 255)
        return false;
    try
    {
      i = Integer.parseInt(paramString.substring(0, 4));
      j = Integer.parseInt(paramString.substring(5, 7));
      k = Integer.parseInt(paramString.substring(8, 10));
    }
    catch (Exception localException1)
    {
      return false;
    }
    int i3 = this.textComponent.getCaretPosition();
    int i6 = 1;
    if ((j > 12) || (j < 1))
    {
      j = Math.min(12, Math.max(1, j));
      i6 = 0;
    }
    if (k < 1)
    {
      k = 1;
      i6 = 0;
    }
    switch (j)
    {
    case 4:
    case 6:
    case 9:
    case 11:
      if (k > 30)
      {
        k = 30;
        i6 = 0;
      }
      break;
    case 2:
      if (((i % 4 == 0) && (i % 100 != 0)) || (i % 400 == 0))
      {
        if (k > 29)
        {
          k = 29;
          i6 = 0;
        }
      }
      else if (k > 28)
      {
        k = 28;
        i6 = 0;
      }
      break;
    case 3:
    case 5:
    case 7:
    case 8:
    case 10:
    default:
      if (k > 31)
      {
        k = 31;
        i6 = 0;
      }
    }
    if (i4 > 10)
    {
      try
      {
        l = Integer.parseInt(paramString.substring(11, 13));
        i1 = Integer.parseInt(paramString.substring(14, 16));
        i2 = Integer.parseInt(paramString.substring(17));
      }
      catch (Exception localException2)
      {
        return false;
      }
      if ((l > 23) || (l < 0))
      {
        l = Math.min(23, Math.max(0, l));
        i6 = 0;
      }
      if ((i1 > 59) || (i1 < 0))
      {
        i1 = Math.min(59, Math.max(0, i1));
        i6 = 0;
      }
      if ((i2 > 59) || (i2 < 0))
      {
        i2 = Math.min(59, Math.max(0, i2));
        i6 = 0;
      }
    }
    if (i6 == 0)
    {
      this.textComponent.setText(toDateString(i, j, k, l, i1, i2));
      this.textComponent.setCaretPosition(i3 + 1);
    }
    return i6;
  }

  private String toDateString(int paramInt1, int paramInt2, int paramInt3, int paramInt4, int paramInt5, int paramInt6)
  {
    paramInt2 = Math.max(1, Math.min(12, paramInt2));
    paramInt3 = Math.max(1, Math.min(31, paramInt3));
    switch (paramInt2)
    {
    case 4:
    case 6:
    case 9:
    case 11:
      paramInt3 = Math.min(30, paramInt3);
      break;
    case 2:
      if (((paramInt1 % 4 == 0) && (paramInt1 % 100 != 0)) || (paramInt1 % 400 == 0))
        paramInt3 = Math.min(29, paramInt3);
      else
        paramInt3 = Math.min(28, paramInt3);
    case 3:
    case 5:
    case 7:
    case 8:
    case 10:
    }
    paramInt4 = Math.max(1, Math.min(24, paramInt4));
    paramInt5 = Math.max(1, Math.min(59, paramInt5));
    paramInt6 = Math.max(1, Math.min(59, paramInt6));
    String str1 = getDateFormat().toPattern();
    String str2 = rPad0(4, "" + paramInt1);
    String str3 = rPad0(2, "" + paramInt2);
    String str4 = rPad0(2, "" + paramInt3);
    String str5 = str2 + str1.substring(4, 5) + str3 + str1.substring(7, 8) + str4;
    if (str1.length() == 19)
      str5 = str5 + str1.substring(10, 11) + rPad0(2, new StringBuffer().append("").append(paramInt4).toString()) + str1.substring(13, 14) + rPad0(2, new StringBuffer().append("").append(paramInt5).toString()) + str1.substring(16, 17) + rPad0(2, new StringBuffer().append("").append(paramInt6).toString());
    return str5;
  }

  private String rPad0(int paramInt, String paramString)
  {
    if (paramString.length() < paramInt)
      paramString = "0" + paramString;
    return paramString;
  }
}